/**
 * 
 */
package com.mths.client.page;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.DoubleClickEvent;
import com.google.gwt.event.dom.client.DoubleClickHandler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.SingleSelectionModel;
import com.mths.client.component.Button;
import com.mths.client.component.ComboBox;
import com.mths.client.component.GridData;
import com.mths.client.component.NumberField;
import com.mths.client.component.TabPanel;
import com.mths.client.component.TextField;
import com.mths.client.constant.FieldType;
import com.mths.client.constant.PageConstant;
import com.mths.client.controller.DocumentController;
import com.mths.client.controller.DocumentControllerAsync;
import com.mths.client.dto.MetaDataDTO;
import com.mths.client.util.AsyncCall;
import com.mths.client.util.PageUtil;

/**
 * @author ibesli
 * 
 */
public class DocumentMetaCreate extends AbstractPage implements IPage {

	private static DocumentCreateMetaUiBinder	uiBinder	= GWT.create(DocumentCreateMetaUiBinder.class);

	interface DocumentCreateMetaUiBinder extends UiBinder<Widget, DocumentMetaCreate> {
	}

	private final DocumentControllerAsync	documentAsync	= GWT.create(DocumentController.class);

	@UiField
	TabPanel								documentTabPnl;

	@UiField
	ComboBox								fieldTypeCmb;

	@UiField
	TextField								fieldNameTxt, fieldIdTxt;

	@UiField
	NumberField								fieldOrderNmbr;

	@UiField
	Button									metaDataSaveBtn, metaDataClearBtn;

	@UiField
	GridData<MetaDataDTO>					metaDataTbl;

	private MetaDataDTO						dto				= new MetaDataDTO();					;

	public DocumentMetaCreate() {
		initWidget(uiBinder.createAndBindUi(this));
		initEventHandler();
		initGridData();
		loadComponentData();
	}

	private void initGridData() {

		// metadata celltable
		metaDataTbl.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);

		TextColumn<MetaDataDTO> fieldNameClm = new TextColumn<MetaDataDTO>() {

			@Override
			public String getValue(MetaDataDTO object) {
				return object.getFieldName();
			}
		};
		fieldNameClm.setSortable(true);

		TextColumn<MetaDataDTO> fieldIdClm = new TextColumn<MetaDataDTO>() {

			@Override
			public String getValue(MetaDataDTO object) {
				return object.getFieldId();
			}
		};

		TextColumn<MetaDataDTO> fieldTypeClm = new TextColumn<MetaDataDTO>() {

			@Override
			public String getValue(MetaDataDTO object) {
				return object.getFieldType();
			}
		};
		TextColumn<MetaDataDTO> fieldOrderClm = new TextColumn<MetaDataDTO>() {

			@Override
			public String getValue(MetaDataDTO object) {
				return object.getFieldOrder().toString();
			}
		};
		metaDataTbl.addColumn(fieldNameClm, "Meta Data Adı");
		metaDataTbl.addColumn(fieldIdClm, "Meta Unique Adı");
		metaDataTbl.addColumn(fieldTypeClm, "Meta Tipi ");
		metaDataTbl.addColumn(fieldOrderClm, "Dizilim Sırası");
		metaDataTbl.setRowCount(10);

		final SingleSelectionModel<MetaDataDTO> selectionModelMetaData = new SingleSelectionModel<MetaDataDTO>();
		metaDataTbl.setSelectionModel(selectionModelMetaData);
		metaDataTbl.addDomHandler(new DoubleClickHandler() {

			@Override
			public void onDoubleClick(DoubleClickEvent event) {
				dto = selectionModelMetaData.getSelectedObject();
				prepareMetaData4Update();
			}

		}, DoubleClickEvent.getType());

	}

	private void initEventHandler() {
		documentTabPnl.selectTab(0);
		documentTabPnl.addSelectionHandler(new SelectionHandler<Integer>() {

			@Override
			public void onSelection(SelectionEvent<Integer> event) {
				if (event.getSelectedItem() == 0) {
					// loadMetaDatas();
				} else if (event.getSelectedItem() == 1) {
					PageUtil.navigationManager.controlOpenPageByFUrl(PageConstant.DOCUMENT_META_PARAM_CREATE);
				} else if (event.getSelectedItem() == 2) {
					PageUtil.navigationManager.controlOpenPageByFUrl(PageConstant.DOCUMENT_CLASS_CREATE);
				} else if (event.getSelectedItem() == 3) {
					PageUtil.navigationManager.controlOpenPageByFUrl(PageConstant.DOCUMENT_TYPE_CREATE);
				} else if (event.getSelectedItem() == 4) {
					PageUtil.navigationManager.controlOpenPageByFUrl(PageConstant.DOCUMENT_MATCH_CREATE);
				}
			}
		});

		metaDataClearBtn.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				refreshMetaDataPage();
			}
		});

		metaDataSaveBtn.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				saveOrUpdateMetaData();
			}
		});

	}

	private void prepareMetaData4Update() {
		fieldNameTxt.setFieldValue(dto.getFieldName());
		fieldIdTxt.setFieldValue(dto.getFieldId());
		fieldTypeCmb.setFieldValue(dto.getFieldType());
		fieldOrderNmbr.setFieldValue(dto.getFieldOrder().toString());
	}

	private void loadComponentData() {

		fieldTypeCmb.addItem(FieldType.TEXT_FIELD);
		fieldTypeCmb.addItem(FieldType.NUMBER_FIELD);
		fieldTypeCmb.addItem(FieldType.COMBOBOX_FIELD);
		fieldTypeCmb.addItem(FieldType.DATE_FIELD);

		loadMetaDatas();
	}

	private void loadMetaDatas() {
		MetaDataDTO dto = new MetaDataDTO();
		documentAsync.loadMetaDatas(dto, new AsyncCall<List<MetaDataDTO>>() {

			@Override
			public void successCall(List<MetaDataDTO> result) {
				// Push the data into the widget.
				if (result != null && !result.isEmpty())
					metaDataTbl.setRowData(result);
			}

		});
	}

	private void saveOrUpdateMetaData() {
		dto.setFieldName(fieldNameTxt.getFieldValue());
		dto.setFieldId(fieldIdTxt.getFieldValue());
		dto.setFieldType(fieldTypeCmb.getFieldValue());
		dto.setFieldOrder(fieldOrderNmbr.getFieldValue() == null ? null : Integer.parseInt(fieldOrderNmbr.getFieldValue()));
		documentAsync.saveMetaData(dto, new AsyncCall<Boolean>() {

			@Override
			public void successCall(Boolean result) {
				loadMetaDatas();
				refreshMetaDataPage();
			}

		});
	}

	private void refreshMetaDataPage() {
		fieldNameTxt.setFieldValue("");
		fieldIdTxt.setFieldValue("");
		fieldTypeCmb.setItemSelected(0, true);
		fieldOrderNmbr.setFieldValue(null);
		dto = new MetaDataDTO();
	}

	@Override
	public String pageName() {
		return messages.DocumentMetaCreate_pageName();
	}
}
